﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace proLearningEnglish
{
    public partial class frmNewSense : Form
    {
        //DataManager objDataManager = null;
        public frmNewSense()
        {
            InitializeComponent();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            string sense = this.txtSense.Text;
            if(frmMain.objDataManager.IsExist(sense.Replace(" ", "_")))
            {
                MessageBox.Show("Nghĩa này đã tồn tại!", "Thông báo");
            }
        }

        private void frmNewSense_Load(object sender, EventArgs e)
        {
            this.Height = 120;
            this.cmbType.Items.AddRange(WordSense.WORDTYPELIST);
            this.cmbType.SelectedIndex = 0;
            this.pictureBox.Tag = "";
        }

        private void txtSense_Leave(object sender, EventArgs e)
        {
            string str = this.txtSense.Text;
            str = str.Trim();
            str = Regex.Replace(str, " +", " ");
            this.txtSense.Text = str;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            WordSense ws = new WordSense(this.cmbType.SelectedIndex, this.txtSense.Text, (string)this.pictureBox.Tag);
            frmMain.objDataManager.addWordSense(ws);
            //frmMain.objDataManager.save();
            //MessageBox.Show("Thêm nghĩa mới thành công!", "Thông báo");
            this.Dispose();
        }

        private void btnAddPicture_Click(object sender, EventArgs e)
        {
            openFileDialog.ShowDialog();
        }

        private void openFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            pictureBox.Tag = openFileDialog.SafeFileName;
            pictureBox.Image = Image.FromFile(openFileDialog.FileName);
            this.Height = 310;
            this.btnDeletePicture.Enabled = true;
        }

        private void btnDeletePicture_Click(object sender, EventArgs e)
        {
            this.pictureBox.Image = null;
            this.pictureBox.Tag = null;
            this.btnDeletePicture.Enabled = false;
            this.Height = 120;
        }
    }
}
